package com.dyz.leetcode;

public class MaxPathSum124 {
    int maxRes = Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        //后序遍历
        postOrder(root);
        return maxRes;
    }

    /**
     * des: 拿到一边，包括该root的最大sum
     * @param root
     * @return
     */
    private int postOrder(TreeNode root){
        if(root==null){return 0;}

        int left = Math.max(postOrder(root.left),0);
        int right = Math.max(postOrder(root.right),0);
        //最关键的二步
        maxRes = Math.max(maxRes, root.val+left+right);
        return root.val+Math.max(left, right);
    }
}
